package com.zdsoft.blp.zongbuju.wode.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.application.MyApp;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.zongbuju.shouye.taoshihui.activity.SHAddressActivity;
import com.zdsoft.blp.zongbuju.shouye.taoshihui.activity.ZhiFuActivity;
import com.zdsoft.blp.zongbuju.wode.activity.WoDeDingDanActivity;
import com.zdsoft.blp.zongbuju.wode.entity.LiuYan;
import com.zdsoft.blp.zongbuju.wode.entity.Order;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/3.
 */
public class OrderAdapter extends BaseAdapter implements NetCallback{
    private List<Order> list;
    private WoDeDingDanActivity activity;
    private LayoutInflater inflater;
    private ViewHodler hodler = null;
    private PopupWindow popupWindow;
    private int index;

    public OrderAdapter(WoDeDingDanActivity activity, List<Order> list) {
        this.activity = activity;
        this.list = list;
        inflater = LayoutInflater.from(activity);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {


        if (convertView == null) {
            convertView = inflater.inflate(R.layout.wodedingdan_item, null);

            hodler = new ViewHodler();

            hodler.iv_picture = (ImageView) convertView.findViewById(R.id.iv_picture);
            hodler.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            hodler.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            hodler.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
            hodler.tv_shuliang = (TextView) convertView.findViewById(R.id.tv_shuliang);
            hodler.bt_shanchu = (Button) convertView.findViewById(R.id.bt_shanchu);
            hodler.bt_fukuan = (Button) convertView.findViewById(R.id.bt_fukuan);

            convertView.setTag(hodler);

        } else {
            hodler = (ViewHodler) convertView.getTag();
        }
        Order order = list.get(position);
        MyApp.setImage(order.getPicPath(), hodler.iv_picture);
        hodler.tv_name.setText(order.getGoodsName());
        hodler.tv_time.setText(order.getCreateOrderTime());
        hodler.tv_price.setText(order.getMoney() + "");
        hodler.tv_shuliang.setText(order.getGoodsCount() + "");
        if (order.isCanDel()) {
            hodler.bt_shanchu.setVisibility(View.VISIBLE);
            hodler.bt_shanchu.setText("删除");
        } else {
            hodler.bt_shanchu.setVisibility(View.GONE);
        }
        if (order.isCanEvaluation()) {
            hodler.bt_fukuan.setVisibility(View.VISIBLE);
            hodler.bt_fukuan.setText("评价");
        }
        if (order.isCanPay()) {
            hodler.bt_fukuan.setVisibility(View.VISIBLE);
            hodler.bt_fukuan.setText("请付款");
        }else {
            hodler.bt_fukuan.setVisibility(View.GONE);
        }
        hodler.bt_fukuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                index =position;
                if ("请付款".equals(hodler.bt_fukuan.getText())) {
                    Intent intent = new Intent(activity, ZhiFuActivity.class);
                    intent.putExtra("goodsName", list.get(position).getGoodsName() + "");
                    intent.putExtra("price", list.get(position).getMoney() + "");
                    intent.putExtra("number", list.get(position).getGoodsCount() + "");
                    intent.putExtra("zPrice", list.get(position).getMoney() + "");
                    intent.putExtra("goodsId", list.get(position).getMerchantGoodsId() + "");
                    //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    activity.startActivity(intent);
                }
            }
        });

        hodler.bt_shanchu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                index =position;
                if ("删除".equals(hodler.bt_shanchu.getText().toString())) {

                    getPopupWindow();
                    backgroundAlpha();
                    // 这里是位置显示方式,在屏幕的上侧
                    popupWindow.showAtLocation(hodler.bt_fukuan, Gravity.CENTER,0,0);

                }
            }
        });

        return convertView;
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
        if(flag){
            try {
                JSONObject object = new JSONObject(res);
                if(object.getBoolean("result")){
                    activity.onRefresh();
                    notifyDataSetChanged();
                }
                Toast.makeText(activity,object.getString("msg").toString(),Toast.LENGTH_SHORT).show();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    class ViewHodler {
        ImageView iv_picture;
        TextView tv_name, tv_time, tv_price, tv_shuliang;
        Button bt_fukuan, bt_shanchu;
    }

    public List<Order> setList(List<Order> list) {
        this.list = list;
        return list;
    }

    private void getPopupWindow() {
        if (null != popupWindow) {
            popupWindow.dismiss();
            return;
        } else {
            initPopuptWindow();
        }
    }

    protected void initPopuptWindow() {
        // TODO Auto-generated method stub
        // 获取自定义布局文件activity_popupwindow_left.xml的视图
        View popupWindow_view = activity.getLayoutInflater().inflate(R.layout.tishiaddress, null, false);
        Button bt_queding = (Button) popupWindow_view.findViewById(R.id.bt_queding);
        Button bt_quxiao = (Button) popupWindow_view.findViewById(R.id.bt_quxiao);
        TextView tv_biaoti = (TextView) popupWindow_view.findViewById(R.id.tv_biaoti);
        tv_biaoti.setText("确定要删除么？");
        bt_quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dissmiss();
            }
        });
        bt_queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Net net = new Net();
                net.url = API.GOODSORDER_URL;
                net.nameSpace = API.GOODSORDER_NAMESPACE;
                net.methodName = API.REMOVEORDER;
                Map<String, Object> map = new HashMap<>();
                map.put("id", list.get(index).getId());
                net.parms = map;
                activity.getServer(net,OrderAdapter.this);
                dissmiss();
            }
        });

        // 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度
        popupWindow = new PopupWindow(popupWindow_view, 900, 400, true);
        // 设置动画效果
        // popupWindow.setAnimationStyle(R.style.AnimationFade);
        // 点击其他地方消失
        /*popupWindow_view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                    popupWindow = null;
                }
                return false;
            }
        });*/
    }

    public void backgroundAlpha() {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = (float) 0.5; //0.0-1.0
        activity.getWindow().setAttributes(lp);
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }

    public void dismissBackgroundAlpha() {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = 1f; //0.0-1.0
        activity.getWindow().setAttributes(lp);
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }

    //让pop消失
    private void dissmiss() {
        popupWindow.dismiss();
        popupWindow = null;
        dismissBackgroundAlpha();
    }
}
